(function(factory) {
	if ( typeof define === "function" && define.amd ) {

		// AMD. Register as an anonymous module.
		define(['HteOS/menu/Menu'],factory);
	} else {
		// Browser globals
		factory();
	}
}(function(){
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.menu.MenuManager 桌面右键菜单管理器
	 * @singleton
	 */
	HteOS.menu.MenuManager = {
		
		
		/**
		 * @property
		 * 当前正在显示的Menu
		 */
		active : null,
		
		/**
		 * @property
		 * 存放所有的Menu
		 */
		menus : {},
		
		/** 
		 * 注册菜单
		 * @param {HteOS.menu.Menu} menu
		 */
		register : function(menu){
			this.menus[menu.id] = menu;
			this.initMenuTrigger(menu);
		},
		
		/** 
		 * 将传入的菜单对象显示出来
		 * @param {HteOS.menu.Menu} menu
		 */
		updateActive : function(menu){
			if(this.active != menu){
				if(this.active){
					this.active.hide();
				}
				this.active = menu;
			}
		},
		
		/**
		 * 隐藏正在显示的菜单
		 * @param {HteOS.menu.Menu} menu
		 */
		clearActive : function(menu){
			if(this.active == menu){
				this.active = null;
			}
		},
		
		/**
		 * 初始化菜单显示触发器，程序自动调用，无需手动调用
		 * @private
		 * @param {HteOS.menu.Menu} menu
		 */
		initMenuTrigger : function(menu){
			var el = menu.getEl();
			if(HteOS.UA.Mobile !== true){
				$(document).on("contextmenu", menu.selector,function(event) {
					var active = HteOS.menu.MenuManager.active;
					if(active){
						active.hide();
					}
					if (event.pageY + el.outerHeight() > $(document)
						.height() - 5) {
						el.css({
							top: event.pageY - el.outerHeight(),
							left: event.pageX
						});
					} else if (event.pageX + el.outerWidth() > $(document)
						.width() - 5) {
						el.css({
							top: event.pageY,
							left: event.pageX - el.outerWidth()
						});
					} else {
						el.css({
							top: event.pageY,
							left: event.pageX
						});
					}
					menu.show(event);
					event.preventDefault();
					event.stopPropagation(); 
				});
			}else{
				$(document).on("longTap", menu.selector,function(event) {
					var active = HteOS.menu.MenuManager.active;
					if(active){
						active.hide();
					}
					menu.show();
					event.preventDefault();
					event.stopPropagation(); 
				});
			}
		},
		
		/**
		 * 初始化右键菜单
		 */
		init : function() {
			var manager = this;
			if(HteOS.UA.Mobile !== true){
				$(document.body).on("click", function(event) {
					if (!$(event.target).hasClass(".hte-menu") && $(event.target).parents(".hte-menu").length == 0) {
						if(manager.active){
							manager.active.hide();
						}
					}
				});
				
				$(document.body).on("mouseenter",".hte-menu-dir",function(){
					var item = $(this), submenu = item.children(".hte-menu-sub"),offset = item.offset();
					submenu.css({
							top: 0,
							left: item.width() - 5
						});
					submenu.show();
					item.addClass("hte-menu-item-active");
				});
				
				$(document.body).on("mouseleave",".hte-menu-dir",function(){
					$(this).find(".hte-menu-sub").hide();
					$(this).removeClass("hte-menu-item-active");
					//menu.find(".hte-menu-sub").hide();
				});
			}else{
				$(document.body).on("tap",".hte-menu-actionsheet-modal", function(event) {
					if(manager.active){
						manager.active.hide();
					}
				});
			}
		}
	};
}));